package gossipinator;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author cox
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.util.*;


public class Gossip {

    private Integer ID;
    private String message,sender,date;
    private static Hashtable hTable = new Hashtable();
    private static Enumeration hte;

    private static int index=0;
    private static IStorage storage;
    //private static IStorage exostore;
    //private static IStorage gspreadstore;

    //This constructor will create gossips without writing them to
    //storage. It is used at midlet initialisation.
    public Gossip(String sender, String message, String indate){
        this.sender = sender;
        this.message = message;
        this.date = indate;
        //ID = new Integer(date.hashCode());
        ID = new Integer(index++);
        hTable.put(ID, this);
        hte = hTable.elements();
    }

    //This constructor creates the gossip object and writes it to
    //storage for safekeeping. It is run each time a new gossip
    //becomes available.
    public Gossip(String sender, String message){
        this.sender = sender;
        this.message = message;
        this.date = new Date().toString();
        //ID = new Integer(date.hashCode());
        ID = new Integer(index++);
        hTable.put(ID, this);
        //refresh hashtable enumeration since we've altered the hashtable
        hte = hTable.elements();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataOutputStream outputDataStream = new DataOutputStream(outputStream);

        //exostore.count();

        //Write the gossip info into storage
        try {
            //index++;
            outputDataStream.writeUTF(this.date);
            outputDataStream.writeUTF(this.sender);
            outputDataStream.writeUTF(this.message);
            outputDataStream.flush();
            storage.write(outputStream,index);
            //now we write it out to bluetooth
            //check if we're client or server
            if (Controller.clientdidntfindserver == true) {
                ServerThread.write(outputStream);
            } else {
                ClientThread.write(outputStream);
            }
            //exostore.write(outputStream,index);
            //exostore.read();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    //At midlet initialisation this function reads out all of the stored info
    //and recreates the gossips and hashtable
    //It creates the storage objects along with the BT server and client
    //threads.
    public synchronized static boolean initGossip() {

        String sender, message, date1;
        StringBuffer buffer = new StringBuffer();
        byte[] byteInputData = new byte[300];
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
        DataInputStream inputDataStream = new DataInputStream(inputStream);


        //Storage Initialisation
        //Options are local record storage and/or storage on exosite.com
        //xmpp record server by way of google talk.

        storage = new RecStorage();

        //exostore = new ExoStorage();
        //exostore.init();
        //here read exostore indexes to see if and how many entries exist...
        //exostore.read();

        try {
            if ( !storage.init()) {
                System.out.println("Storage init returned false, nothing to init...");
                return false;
            }

            while (storage.hasnext()) {
                inputDataStream = new DataInputStream(storage.read());
                date1 = new String(inputDataStream.readUTF());
                sender = new String(inputDataStream.readUTF());
                message = new String(inputDataStream.readUTF());
                System.out.println("Storage entry: "+sender+" "+message);
                new Gossip(sender,message,date1);
            }

            return true;
        } catch(Exception e) {
            System.out.println(e);
        }
        return false;
    }

    public static Gossip getGossip(){

       if (hte==null) return null;

       if (hte.hasMoreElements()) {
           return (Gossip)hte.nextElement();
       } else {
           return null;
       }

    }

    public static void killHash() {
        hTable.clear();
        storage.deinit();
    }

    public String getHeader() {
        return (this.date +" "+this.sender);
    }

    public String getMessage() {
        return this.message;
    }

}
